﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JustTest
{
    public partial class Tests : System.Web.UI.Page
    {
        private static JustTestDb context = new JustTestDb();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownCategories_SelectedIndexChanged(sender, e);
            }
            
        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<JustTest.Models.Category> DropDownCategories_GetData()
        {
            var categories = context.Categoriess;
            return categories;
        }

        protected void DropDownCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridViewTests.DataBind();
        }

        protected void ListViewTests_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewTests.PageIndex = e.NewPageIndex;
            DropDownCategories_SelectedIndexChanged(sender, e);
        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<JustTest.Models.Test> ListViewTests_GetData()
        {
            string categoryIdStr = this.DropDownCategories.SelectedValue;
            if (categoryIdStr == String.Empty)
            {
                categoryIdStr = "1";
            }

            int categoryId = int.Parse(categoryIdStr);
          
            return context.Tests.Include("User")
                .Where(test => test.Category.Id == categoryId 
                    && test.Status == TestStatus.Active)
                .OrderBy(t => t.Id);
        }
    }
}